package com.authine.cloudpivot.ext.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.authine.cloudpivot.ext.service.SyncOrgSevice;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.view.ResponseResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 同步组织架构
 *
 * @author TED
 * @author shinka
 * @date 2021/07/13
 */
@Api(value = "01::EXTAPI::同步组织架构", tags = "01::EXTAPI::同步组织架构")
@RestController
@RequestMapping("/ext/syncorg")
@Slf4j
@Validated
@Component
public class SyncOrgController extends BaseController {

    @Resource
    private SyncOrgSevice syncOrgSevice;

    /**
     * 功能描述: 全量同步组织架构V2
     *
     * @author TED
     * @date 2021年6月23日
     * @version v2.0
     */
    @ApiOperation(value = "全量同步组织架构V2")
    @GetMapping(value = "/syncOrgAllV2")
    public ResponseResult<Map<String, Object>> syncOrgAllV2() {
        long startTime = System.currentTimeMillis();
        Map<String, Object> result = syncOrgSevice.syncOrgAllV2(getUserId());
        long endTime = System.currentTimeMillis();
        if ((boolean)result.get("status")) {
            log.info("同步全量组织机构V2共花费了" + ((endTime - startTime) / 1000) + "秒时间！");
        } else {
            log.error("同步全量组织机构V2出错！");
        }
        return getOkResponseResult(result, "同步全量组织机构模块运行成功!");
    }

    /**
     * 功能描述: 增量同步组织架构V2
     *
     * @author TED
     * @date 2021年6月23日
     * @version v2.0
     */
    @ApiOperation(value = "增量同步组织架构V2")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptartmentId", value = "部门ID", required = true, paramType = "query",
        dataType = "String")})
    @GetMapping(value = "/syncOrgIncrementV2")
    public ResponseResult<Map<String, Object>> syncOrgIncrementV2(String deptartmentId) {
        long startTime = System.currentTimeMillis();
        Map<String, Object> result = syncOrgSevice.syncOrgIncrementV2(getUserId(), deptartmentId);
        long endTime = System.currentTimeMillis();
        if ((boolean)result.get("status")) {
            log.info("同步增量组织机构V2共花费了" + ((endTime - startTime) / 1000) + "秒时间！");
        } else {
            log.error("同步增量组织机构V2出错！");
        }
        return getOkResponseResult(result, "增量同步组织机构模块V2运行成功!");
    }

    @ApiOperation(value = "同步用户V2。只同步用户，前提保证组织机构正确，否则请先同步组织机构。")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptartmentId", value = "部门ID", required = false, paramType = "query",
        dataType = "String")})
    @GetMapping(value = "/syncUsersV2")
    public ResponseResult<Map<String, Object>> syncUsersV2(String deptartmentId) {
        long startTime = System.currentTimeMillis();
        Map<String, Object> result = syncOrgSevice.syncUsersV2(getUserId(), deptartmentId);
        long endTime = System.currentTimeMillis();
        if ((boolean)result.get("status")) {
            log.info("同步用户V2共花费了" + ((endTime - startTime) / 1000) + "秒时间！");
        } else {
            log.error("同步用户V2出错！");
        }
        return getOkResponseResult(result, "同步用户V2模块运行成功!");
    }

    @ApiOperation(value = "更新queryCode")
    @GetMapping(value = "/updateQueryCode")
    public ResponseResult<Map<String, Object>> updateQueryCode() {
        long startTime = System.currentTimeMillis();
        syncOrgSevice.updateQueryCode();
        long endTime = System.currentTimeMillis();
        log.info("更新queryCode共花费了" + ((endTime - startTime) / 1000) + "秒时间！");

        return getOkResponseResult(null, "更新queryCode运行成功!");
    }

    @ApiOperation(value = "更新queryCodeV2")
    @GetMapping(value = "/updateQueryCodeV2")
    public ResponseResult<Map<String, Object>> updateQueryCodeV2() {
        long startTime = System.currentTimeMillis();
        syncOrgSevice.updateQueryCodeV2();
        long endTime = System.currentTimeMillis();
        log.info("更新queryCodeV2共花费了" + ((endTime - startTime) / 1000) + "秒时间！");

        return getOkResponseResult(null, "更新queryCodeV2运行成功!");
    }

}
